/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.yonsei.iwb.sdt.refinement.generator;

import com.hp.hpl.jena.rdf.model.ResIterator;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.vocabulary.RDF;
import java.util.Vector;
import edu.yonsei.iwb.sdt.datastructure.DecomposeCandidateSet;
import edu.yonsei.iwb.sdt.datastructure.TargetInstance;
import edu.yonsei.iwb.sdt.datastructure.tree.BranchingType;
import edu.yonsei.iwb.sdt.refinement.ConceptConstructorRefinement;
import edu.yonsei.iwb.sdt.refinement.DLConstructors;
import edu.yonsei.iwb.sdt.refinement.Refinement;
import edu.yonsei.iwb.ts.TSSearch;
import java.io.IOException;
import java.util.ArrayList;

/**
 *
 * @author JDK
 */
public class ConceptConstructorRefinementMaker extends Thread {

    private Vector<TargetInstance> instanceSet;
    private Vector<String> classes;
    private DecomposeCandidateSet dcs;

    public ConceptConstructorRefinementMaker(Vector<TargetInstance> instanceSet) {
        this.instanceSet = instanceSet;
    }

    @Override
    public void run() {
    }

    /**
     * 다수의 클래스로 한번에 분할
     *
     * @param classList 한번에 분할되는 클래스 집합
     * @return
     */
    public Vector<Refinement> refinementGenerator(ArrayList<String> classList) throws IOException {        

        Vector<Refinement> rset = new Vector<Refinement>();
        Vector<TargetInstance> tiCloneSet = new Vector(instanceSet);
        Vector<String> subset = new Vector();

        for (int i = 0; i < classList.size(); i++) {
            String classname = classList.get(i);
            Vector<TargetInstance> sfInsSet = new Vector();

            Refinement pref = new ConceptConstructorRefinement("", classname);
            for (int j = 0; j < tiCloneSet.size(); j++) {
                TargetInstance ti = tiCloneSet.get(j);
                if (ti.checkClassType(classname)) {
                    sfInsSet.add(ti);
                }
            }

            if (!sfInsSet.isEmpty()) {
                for (int j = 0; j < sfInsSet.size(); j++) {
                    TargetInstance ati = sfInsSet.get(j);
                    tiCloneSet.remove(ati);
                }
                pref.setInstanceSet(sfInsSet);
                rset.add(pref);
            }
        }

        if (!tiCloneSet.isEmpty()) {
            Refinement nref = new ConceptConstructorRefinement(classList);
            nref.setBranchingType(BranchingType.NEGATION_TYPE);
            nref.setInstanceSet(tiCloneSet);
            rset.add(nref);
        }

        return rset;
    }

    /**
     * find ccr for given resource r
     *
     * @param classname
     * @return
     */
    public Vector<Refinement> refinementGenerator(String classname) {
        Vector<Refinement> rset = new Vector<Refinement>();
        Vector<TargetInstance> remainTIset = new Vector(instanceSet);

        Refinement pref = new ConceptConstructorRefinement("", classname);
        Refinement nref = new ConceptConstructorRefinement(DLConstructors.NEGATION, classname);
        nref.setBranchingType(BranchingType.NEGATION_TYPE);
        
        Vector<TargetInstance> satisfyTIset = new Vector();        
        for (int i = 0; i < instanceSet.size(); i++) {
            TargetInstance ti = instanceSet.get(i);
            if(ti.checkClassType(classname)){
                satisfyTIset.add(ti);                
                remainTIset.remove(ti);
            }            
        }
        
        if(!satisfyTIset.isEmpty()){
            pref.setInstanceSet(satisfyTIset);
            rset.add(pref);
        }

        if (!remainTIset.isEmpty()) {
            nref.setInstanceSet(remainTIset);            
            rset.add(nref);
        }
        return rset;
    }
}
